package com.example.administrator.fishbone.Fragment;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.Path;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.fishbone.R;
import com.example.administrator.fishbone.bean.DaoTask;
import com.example.administrator.fishbone.utils.DateTimePickDialogUtil;
import com.example.administrator.fishbone.bean.Person;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.james.biuedittext.BiuEditText;

/**
 * Created by ${杨文超} on 2016/8/31.
 */
public class HomeAddFragment extends Fragment implements View.OnClickListener{
ImageButton imageButton;
ImageButton imageButtonacamera;
    ImageButton imageButtonfile;
    ImageButton imagephoto;

    BiuEditText biuEditText;
    TextView textView;

    //链接
    TextView linktext;
   ImageView imageView;

    //时间选择器
    EditText starttime;
    EditText overtime;
    private String initStartDateTime = "2016年9月1日 19:35";
    private String initOverDateTime = "2017年9月1日 19:36";

    TextView textViewa;

    ImageButton text1,text2,text3;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_add, container, false);
//        imageButton = (ImageButton) view.findViewById(R.id.add_task_imgv_principal);
//       imageButton.setOnClickListener(this);
        imageButtonacamera = (ImageButton) view.findViewById(R.id.home_add_fot_camera);
        imageButtonfile = (ImageButton) view.findViewById(R.id.home_add_fot_file);
        imagephoto = (ImageButton) view.findViewById(R.id.home_add_fot_photo);
        imagephoto.setOnClickListener(this);
        imageButtonfile.setOnClickListener(this);
        imageButtonacamera.setOnClickListener(this);
        textView = (TextView) view.findViewById(R.id.home_add_bao);
        textView.setOnClickListener(this);
        biuEditText = (BiuEditText) view.findViewById(R.id.add_task_edit);
        text1 = (ImageButton) view.findViewById(R.id.add_task_imgv_principal);
        text1.setOnClickListener(this);
        text2 = (ImageButton) view.findViewById(R.id.add_task_imgv_reviewer);
        text2.setOnClickListener(this);
        text3 = (ImageButton) view.findViewById(R.id.add_task_imgv_cc);
        text3.setOnClickListener(this);


        linktext = (TextView) view.findViewById(R.id.add_task_imgv_adjunct);
        imageView = (ImageView) view.findViewById(R.id.home_add_gridow);

        linktext.setOnClickListener(this);

        starttime = (EditText) view.findViewById(R.id.add_task_time_from);
        overtime = (EditText) view.findViewById(R.id.add_task_time_to);
        DateTime();
        return view;
    }

    private void DateTime() {
        starttime.setText(initStartDateTime);
        overtime.setText(initOverDateTime);

        starttime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                DateTimePickDialogUtil dateTimePickDialogUtil = new DateTimePickDialogUtil(
                        HomeAddFragment.this, initOverDateTime);
                dateTimePickDialogUtil.dateTimePickerDialog(starttime);

            }
        });

        overtime.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
                        HomeAddFragment.this, initOverDateTime);
                dateTimePicKDialog.dateTimePickerDialog(overtime);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.add_task_imgv_principal:
//                FragmentManager fm = getActivity().getSupportFragmentManager();
//                FragmentTransaction transaction = fm.beginTransaction();
//                transaction.add(android.R.id.content, new PersonnelFragment(), "PersonnelFragment");
//                transaction.addToBackStack("PersonnelFragment");
//                transaction.commit();
//                break;
            case R.id.home_add_fot_camera:
                //调用相机
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, getActivity().DEFAULT_KEYS_DIALER);

                break;

            case R.id.home_add_fot_file:
                //调用文件夹
                Intent intentfile = new Intent(Intent.ACTION_GET_CONTENT);
                intentfile.setType("*/*");
                intentfile.addCategory(Intent.CATEGORY_OPENABLE);
                try {
                    startActivityForResult(Intent.createChooser(intentfile, "请选择一个要上传的文件"),
                            getActivity().DEFAULT_KEYS_DIALER);
                } catch (android.content.ActivityNotFoundException ex) {
                    // Potentially direct the user to the Market with a Dialog
                    Toast.makeText(getActivity(), "请安装文件管理器", Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            case R.id.home_add_fot_photo:
                //调用相册
                Intent intentphoto = new Intent(Intent.ACTION_GET_CONTENT);
                intentphoto.addCategory(Intent.CATEGORY_OPENABLE);
                intentphoto.setType("image/*");
                startActivityForResult(intentphoto,0);
                break;
            case R.id.home_add_bao:
                SimpleDateFormat formata = new SimpleDateFormat("MM-dd");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formata.format(curDate);

                String date = biuEditText.getText().toString();
                List<Person> tasks = new ArrayList<>();
                    Person task = new Person("sd",date,date,str,"yan","Do",str,str);
                    tasks.add(task);


                boolean isSuccess = DaoTask.instance().insert(tasks);
                if (isSuccess) {
                    Toast.makeText(getActivity(),"插入数据成功",Toast.LENGTH_LONG).show();
                }
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.remove(HomeAddFragment.this);
                ft.commit();

                break;
            case R.id.add_task_imgv_adjunct:
                imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.add_task_imgv_principal:
                jumpToContacts(1);
                break;
            case R.id.add_task_imgv_reviewer:
                jumpToContacts(2);
                break;
            case R.id.add_task_imgv_cc:
                jumpToContacts(3);
                break;
        }
    }

    /**
     * 相机路径
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //共同（相机，文件夹自定）
        System.out.println("--->RESULT_OK=" + getActivity().RESULT_OK);
        System.out.println("--->RESULT_CANCELED=" + getActivity().RESULT_CANCELED);
        System.out.println("--->RESULT_FIRST_USER=" + getActivity().RESULT_FIRST_USER);
        System.out.println("--->resultCode=" + resultCode);

        //相机归属--
        if (resultCode == getActivity().RESULT_OK) {
            System.out.println("--->2");
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
            imageView.setImageBitmap(bitmap);
            imageView.setVisibility(View.VISIBLE);
            }
//文件夹--
//            if (requestCode == getActivity().RESULT_OK) {
//                Uri uri = data.getData();
//                String[] proj = {MediaStore.Images.Media.DATA};
//                Cursor actualimagecursor = getActivity().managedQuery(uri, proj, null, null, null);
//                int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//                actualimagecursor.moveToFirst();
//                String img_path = actualimagecursor.getString(actual_image_column_index);
//                File file = new File(img_path);
//                textViewa.setText(file.getAbsolutePath());
//
//                Toast.makeText(getActivity(), "============>" + file.toString(), Toast.LENGTH_SHORT).show();
//            }

            super.onActivityResult(requestCode, resultCode, data);
        }


//
//    @Override
//    public boolean onBackPressed() {
//        return false;
//    }



    private void jumpToContacts(int i) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment fragment;
        FragmentTransaction ft = fm.beginTransaction();
        fragment = new PersonnelFragment();
        switch (i) {
            case 1:
                ft.add(android.R.id.content, fragment, "pp");
                ft.addToBackStack("pp");
                ft.commit();
                break;
            case 2:
                ft.add(android.R.id.content, fragment, "ppo");
                ft.addToBackStack("ppo");
                ft.commit();
                break;
            case 3:
                ft.add(android.R.id.content, fragment, "ddd");
                ft.addToBackStack("ddd");
                ft.commit();
        }
    }

}
